package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Role;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.RoleService;

public class MemberCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("MemberCreateProcess");

	private String email;
	private String password;
	private String confirmPassword;
	private String firstname;
	private String lastname;
	private String description;
	private String gender;
	private int age;
	private String country;
	private String state;
	private String city;
	private int contactVisible;
	private int enabled;
	private String photo;

	private MemberService memberService;
	private RoleService roleService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		MemberCreateProcess.logger = logger;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getContactVisible() {
		return contactVisible;
	}

	public void setContactVisible(int contactVisible) {
		this.contactVisible = contactVisible;
	}

	public int getEnabled() {
		return enabled;
	}

	public void setEnabled(int enabled) {
		this.enabled = enabled;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public int initCheck() {
		if (memberService == null || roleService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (memberService.hasMember(email)) {
			setErrorMessage("Member " + email + " already exists!");
			return TeamerGlobalVariables.FAILED;
		}

		if (!password.equals(confirmPassword)) {
			setErrorMessage("Password has to be same!");
			return TeamerGlobalVariables.FAILED;
		}
		return TeamerGlobalVariables.SUCCESS;
	}
	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		// create member
		Member member = new Member();
		member.setEmail(email);
		member.setPassword(password);
		member.setFirstname(firstname);
		member.setLastname(lastname);
		member.setDescription(description);
		member.setGender(gender);
		member.setAge(age);
		member.setCountry(country);
		member.setState(state);
		member.setCity(city);
		member.setContactVisible(contactVisible);
		member.setEnabled(TeamerGlobalVariables.ENABLED);
		member.setPhoto(photo);

		Role role = new Role();
		role.setEmail(email);
		role.setRoleGroup(TeamerGlobalVariables.MEMBER);
		role.setRoleName(TeamerGlobalVariables.ROLE_USER);

		try {
			memberService.addMember(member);
			roleService.addRole(role);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Member: " + email + " cannot be saved!");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
